package com.hotline.communication.aspect;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hotline.communication.service.AppInfoService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Objects;

@Slf4j
@Aspect
@Component
public class TokenValidateAspect {

    @Autowired
    private AppInfoService appInfoService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Pointcut("@annotation(com.hotline.communication.annotation.TokenValidate)")
    public void doAspect(){

    }

    /**
     * 注解校验参数非空,只校验第一个参数
     *
     * @param point
     */
    @Before("doAspect()")
    public void doBefore(JoinPoint point){
        try {
            //入参
//            Object[] args = point.getArgs();
//            if (null == args || args.length == 0) {
//                throw new RuntimeException("未设置校验参数！");
//            }
//            Object param = args[0];
//            JSONObject tokenJson = JSON.parseObject(JSONObject.toJSONString(param));
//            TokenDTO tokenDTO = null;
//            String  tokenStr= (String) redisTemplate.opsForValue().get("appToken:"+tokenJson.getString("appID"));
//            if(StringUtils.isNotBlank(tokenStr)){
//                tokenDTO=JSON.parseObject(tokenStr, TokenDTO.class);
//            }
//            if(Objects.isNull(tokenDTO)) {
//                throw new RuntimeException("appId不正确！");
//            }
//            if(!StringUtils.equals(tokenJson.getString("appSecret"),tokenDTO.getAppSecret())){
//                throw new RuntimeException("appSecret不正确！");
//            }
//            if(!StringUtils.equals(tokenDTO.getAppToken(),tokenJson.getString("appToken"))){
//                throw new RuntimeException("appToken不正确！");
//            }
        } catch (Exception e) {
            log.error("校验参数失败:{}",e);
            throw e;
        }
    }

}
